<template>
  <el-dialog :title="title" :visible.sync="visiable" width="30%" :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="80px"  >
      <el-form-item label="标签名称:">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="分类：" prop="categoryList" >
        <el-select v-model="checkedCategoryId" clearable filterable style="width:120px;"  >
          <el-option v-for="item in categoryList" :key="item.categoryId" :label="item.name" :value="item.categoryId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" >
        <el-button type="primary" @click="submit">确认</el-button>
        <el-button type="danger" @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import blogApi from '@/api/blog';
export default {
  props:{
    labelId: null,  // 标签id
    isEdit:{  // 是否编辑页面
      type: Boolean,
      default: true
    },
    title:{
      type:String,
      default:''
    },
    visiable:{  // 弹出窗口
      type:Boolean,
      default:false
    },
    formData: { // 提交表单数据
      type:Object,
      default:{}
    },
    remoteClose: Function, //用于关闭窗口
  },

  data() {
    return {
      categoryList: [], // 分类列表
      checkedCategoryId: '', // 选中的分类
    }
  },

  watch: {
    // 监听visiable的变化 将改变之后的值传入
    visiable(val) {
      if(val){
        this.fetchCategory();
        this.checkedCategoryId = this.formData.categoryId;
      }
    },

    checkedCategoryId (newId, oldId) {
      console.log('改变了分类id')
      console.log(oldId + "====>" + newId)
      this.checkedCategoryId = newId;
    }
  },

  methods: {
    // 获取所有的分类信息
    fetchCategory() {
      blogApi.getAllCategory().then(response => {
        if (response.code === 200) {
          this.categoryList = response.data;
        }
      })
    },

    // 提交
    submit() {
      this.formData.categoryId = this.checkedCategoryId;
      if (this.isEdit) {
        // 提交编辑
        blogApi.updateLabel(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '该分类下有标签或文章引用，不能删除',
              duration: 1500
            });
          }
          this.handleClose();
        })
      } else {
        // 提交新增
        console.log('新增时选择的分类id ===' + this.formData.categoryId);
        blogApi.addLabel(this.formData).then(response => {
          if (response.code === 200) {
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 1500
            });
          } else if (response.code === 306) {
            this.$notify.error({
              title: '错误',
              message: '您没有操作权限，请联系管理员',
              duration: 1500
            });
          } else {
            this.$notify.error({
              title: '错误',
              message: '系统异常，请稍后再试',
              duration: 1500
            });
          }
          this.handleClose();
        })
      }
    },

    // 关闭窗口
    handleClose() {
      // 调用父组件的方法关闭窗口
      this.formData.name = '';
      this.remoteClose();
    },
  },
}
</script>

<style scoped>

</style>
